package com.atguigu.gulimall.order.config;

/**
 * @Author: fd
 * @Date 2021/10/11 16:07
 * @Description: 支付宝支付工具类
 */
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
@Slf4j
public class AlipayTemplate {

	// 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
	public String app_id;

	// 商户私钥，您的PKCS8格式RSA2私钥
	public String merchant_private_key;

	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
	public String alipay_public_key;

	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	// 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
	public String notify_url;

	// 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	//同步通知，支付成功，一般跳转到成功页
	public String return_url;

	// 签名方式
	private  String sign_type;

	// 字符编码格式
	private  String charset;

	//订单超时时间
	private String timeout = "1m";

	// 支付宝网关； https://openapi.alipaydev.com/gateway.do
	public String gatewayUrl;

	/**
	 * 支付宝支付
	 * @author fd
	 * @date 2021/10/12 15:12
	 * @param vo
	 * @return java.lang.String
	 */
	public  String pay(PayVo vo) throws AlipayApiException {

		//AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
		//1、根据支付宝的配置生成一个支付客户端
		AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
				app_id, merchant_private_key, "json",
				charset, alipay_public_key, sign_type);

		//2、创建一个支付请求 //设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(return_url);
		alipayRequest.setNotifyUrl(notify_url);

		//商户订单号，商户网站订单系统中唯一订单号，必填
		String out_trade_no = vo.getOut_trade_no();
		//付款金额，必填
		String total_amount = vo.getTotal_amount();
		//订单名称，必填
		String subject = vo.getSubject();
		//商品描述，可空
		String body = vo.getBody();

		alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
				+ "\"total_amount\":\""+ total_amount +"\","
				+ "\"subject\":\""+ subject +"\","
				+ "\"body\":\""+ body +"\","
				+ "\"timeout_express\":\""+timeout+"\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

		String result = alipayClient.pageExecute(alipayRequest).getBody();
		//会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
		log.info("支付宝的响应：{}" + result);
		return result;
	}

	/**
	 * 支付宝手动关闭订单
	 * @author fd
	 * @date 2021/10/12 15:12
	 * @param orderEntity
	 * @return java.lang.String
	 */
	public String closeAliPay(OrderEntity orderEntity) throws AlipayApiException {
		// 获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
				app_id, merchant_private_key, "json",
				charset, alipay_public_key, sign_type);
		// 设置请求参数
		AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
		//商户订单号，商户网站订单系统中唯一订单号
		String out_trade_no = orderEntity.getOrderSn();
		alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"}");
		//请求
		String result = alipayClient.execute(alipayRequest).getBody();
		log.info("手动关单支付宝的响应：{}" + result);
		return result;
	}
}